package models

import (
	"github.com/liangchao/blogo/src/common/global"
	"time"
)

type User struct {
	AdminUserId   int    `json:"admin_user_id" gorm:"primaryKey"`
	LoginUserName string `json:"login_user_name"`
	LoginPassword string `json:"login_password"`
	NickName      string `json:"nick_name"`
	Locked        int    `json:"locked"`
}

func (*User) TableName() string {
	return "tb_admin_user"
}

// FindUserById 通过id查询用户
func (u *User) FindUserById() (*User, error) {
	err := global.DB.Where("admin_user_id = ?", u.AdminUserId).Find(u).Error
	return u, err
}

// UserBlogRelation 用户博客关系表
type UserBlogRelation struct {
	RelationId    int        `gorm:"relation_id primaryKey" json:"relation_id"`
	BlogId        int        `gorn:"blog_id" json:"blog_id"`
	AdminUserId   int        `gorm:"admin_user_id" json:"admin_user_id"`
	CreateTime    *time.Time `gorm:"create_time" json:"create_time"`
	CreateTimeStr string     `gorm:"create_time_str" json:"create_time_str"`
}

func (*UserBlogRelation) TableName() string{
	return "tb_admin_user_blog_relation"
}

// FindUidByBid 通过 bid 查 uid
func (r *UserBlogRelation) FindUidByBid(bid int) (uid *User, err error) {
	err = global.DB.Where("blog_id = ?", bid).Find(r).Error
	uid = &User{AdminUserId: r.AdminUserId}
	return
}
